﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using System.Threading;

namespace MP3Ganizer.Modules.Visualisation.ViewModels
{
    /// <summary>
    /// ViewModel du Visualisation
    /// </summary>
    public class VisualisationVM : INotifyPropertyChanged, IVisualisationVM
    {
        #region Fields
        private readonly IUnityContainer container;
       #endregion

        

        /// <summary>
        /// Constructeur. Les paramètres sont pré-remplis par le UnityContainer.
        /// </summary>
        /// <param name="container"></param>
        /// <param name="mainView"></param>
        public VisualisationVM(IUnityContainer container, MP3Ganizer.Modules.Visualisation.Views.IVisualisationDefaultV defaultView, MP3Ganizer.Modules.Visualisation.Views.IVisualisationImageV imageView, MP3Ganizer.Modules.Visualisation.Views.IVisualisationAudioV audioView)
        {
            this.container = container;
            this.DefaultView = defaultView;
            this.ImageView = imageView;
            this.AudioView = audioView;
            this.DefaultView.SetModel(this);
            this.ImageView.SetModel(this);
            this.AudioView.SetModel(this);
            this.container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Subscribe(OnUserAction, Microsoft.Practices.Composite.Presentation.Events.ThreadOption.UIThread, true);
        }

        private void OnUserAction(MP3Ganizer.Events.User.UserActionEventArgs e)
        {
            if (e.Action == MP3Ganizer.Common.Enums.UserAction.ShowMedia)
            {
                if (e.Data is Events.User.ShowMediaData)
                {
                    //OnControllerChanged(e);
                }
            }
        }

        public void OnControllerChanged(Events.User.UserActionEventArgs e)
        {
            Events.User.ShowMediaData data = e.Data as Events.User.ShowMediaData;
            _album = data.Album;
            _artiste = data.Artiste;
            _path = data.Path;
            _title = data.Titre;
            OnPropertyChanged("Artiste");
            OnPropertyChanged("Album");
            OnPropertyChanged("Path");
            OnPropertyChanged("Title");
            if(data.DataType == Common.Enums.DataType.Image)
                 ImageView.Show();
            
            if (e.CallBackMethod != null)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                e.CallBackMethod();
            }
        }

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Implémentation de INotifyPropertyChanged.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fonction de Raise de l'évènement PropertyChanged.
        /// </summary>
        /// <param name="PropertyName"></param>
        protected void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        #endregion

        #region Membres IVisualisationVM
        private string _path;
        private string _artiste;
        private string _album;
        private string _title;
        private Common.Enums.DataType  _dataType;

        public Views.IVisualisationDefaultV DefaultView { get; private set; }
        public Views.IVisualisationAudioV AudioView { get; private set; }
        public Views.IVisualisationImageV ImageView { get; private set; }
        public string Path
        {
            get
            {
                return _path;
            }
            set { _path=Path; }
        }

        public string Artiste
        {
            get
            {
                return _artiste;
            }
        }

        public string Album
        {
            get
            {
                return _album;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
        }

        public Common.Enums.DataType DataType
        {
            get
            {
                return _dataType;
            }
        }
        #endregion


        
    }
}

